
package com.heimali.happyreader.ui.activity;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.heimali.happyreader.HappyReaderApplication;
import com.heimali.happyreader.R;
import com.heimali.happyreader.ui.fragment.CommonListFragment;
import com.heimali.happyreader.ui.fragment.PersonalListFragment;

import java.util.ArrayList;

public class ListActivity extends FragmentActivity implements OnClickListener {

    public static String TAG = "ListActivity";
    public final String FRAGMENT_SHOW_INDEX = "fragment_show_index";

    //当前显示的Tab
    public static ShowType curShowType = null;

    public enum ShowType {
        COMMON_LIST, PERSONAL_LIST
    };

    private ViewPager mViewPager;
    private CommonListFragment mCommonListFragment;
    private PersonalListFragment mPersonalListFragment;
    
    private Button mCommonTabBtn;
    private Button mPersonalTabBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        initView();
        initViewPager();
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        int lastIndex = savedInstanceState.getInt(FRAGMENT_SHOW_INDEX);
        changeFragment(findTypeByIndex(lastIndex));
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(FRAGMENT_SHOW_INDEX, findIndexByType(curShowType));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (ShowType.COMMON_LIST == curShowType) {
                return mCommonListFragment.onKeyDown(keyCode, event);
            } else if (ShowType.PERSONAL_LIST == curShowType && mPersonalListFragment != null) {
                return mPersonalListFragment.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.title_btn_common:
                mViewPager.setCurrentItem(findIndexByType(ShowType.COMMON_LIST));
                break;
            case R.id.title_btn_personal:
                mViewPager.setCurrentItem(findIndexByType(ShowType.PERSONAL_LIST));
                break;
        }
    }

    private void initView() {
        //((TextView) findViewById(R.id.top_title)).setText(R.string.app_name);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mCommonTabBtn = (Button) findViewById(R.id.title_btn_common);
        mCommonTabBtn.setOnClickListener(this);
        mPersonalTabBtn = (Button) findViewById(R.id.title_btn_personal);
        mPersonalTabBtn.setOnClickListener(this);
    }

    private void initViewPager() {
        ArrayList<Fragment> list = new ArrayList<Fragment>();
        mCommonListFragment = new CommonListFragment();
        list.add(mCommonListFragment);
        mPersonalListFragment = new PersonalListFragment();
        list.add(mPersonalListFragment);
        mViewPager.setAdapter(new HomeFragmentPagerAdapter(list));
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        mViewPager.setOffscreenPageLimit(1);
    }

    /**
     * 根据类型返回对应索引值
     * 
     * @param type
     * @return
     */
    public static int findIndexByType(ShowType type) {
        if (ShowType.COMMON_LIST == type) {
            return 0;
        } else if (ShowType.PERSONAL_LIST == type) {
            return 1;
        } else {
            return -1;
        }
    }

    /**
     * 根据索引返回对应类型
     * @param index
     * @return
     */
    public static ShowType findTypeByIndex(int index) {
        switch (index) {
            case 1:
                return ShowType.PERSONAL_LIST;
            default:
                return ShowType.COMMON_LIST;
        }
    }

    /**
     * 切换Fragment
     * 
     * @param type
     */
    public void changeFragment(ShowType type) {
        
        if (type == curShowType) {
            return ;
        }
        
        ColorStateList selectedColor = getResources().getColorStateList(R.color.gray333);
        ColorStateList unSelectedColor = getResources().getColorStateList(R.color.gray);
        
        HappyReaderApplication.lastExitTime = 0;
        curShowType = type;
        if (ShowType.COMMON_LIST == type) {
            HappyReaderApplication.curUserInfo = HappyReaderApplication.commonUserInfo;
            mCommonTabBtn.setBackgroundResource(R.drawable.title_tab_btn_left_selected_bg);
            mCommonTabBtn.setTextColor(selectedColor);
            mPersonalTabBtn.setBackgroundResource(R.color.transparent);
            mPersonalTabBtn.setTextColor(unSelectedColor);
            mCommonListFragment.onShow();
            mPersonalListFragment.onHide();
        } else if (ShowType.PERSONAL_LIST == type) {
            HappyReaderApplication.curUserInfo = HappyReaderApplication.loginUserInfo;
            mPersonalTabBtn.setBackgroundResource(R.drawable.title_tab_btn_right_selected_bg);
            mPersonalTabBtn.setTextColor(selectedColor);
            mCommonTabBtn.setBackgroundResource(R.color.transparent);
            mCommonTabBtn.setTextColor(unSelectedColor);
            mCommonListFragment.onHide();
            mPersonalListFragment.onShow();
        }
    }

    public class HomeFragmentPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> mFragmentsList;

        public HomeFragmentPagerAdapter(ArrayList<Fragment> fragments) {
            super(getSupportFragmentManager());
            mFragmentsList = fragments;
        }

        @Override
        public int getCount() {
            if (mFragmentsList == null) {
                return 0;
            }
            return mFragmentsList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentsList.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }
    }

    public class MyOnPageChangeListener implements OnPageChangeListener {

        public void onPageSelected(int index) {
            switch (index) {
                case 0:
                    changeFragment(ShowType.COMMON_LIST);
                    break;
                case 1:
                    changeFragment(ShowType.PERSONAL_LIST);
                    break;
            }
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        public void onPageScrollStateChanged(int index) {
        }
    }

}
